package cn.kgc.housedemo01.controller;

import cn.kgc.housedemo01.pojo.HouseInfo;
import cn.kgc.housedemo01.pojo.HouseType;
import cn.kgc.housedemo01.pojo.ResultEntity;
import cn.kgc.housedemo01.pojo.viewsInfoType;
import cn.kgc.housedemo01.service.HouseService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
@Api(tags = "房屋信息接口",description = "提供房屋相关的REST API")
public class HouseController {
    @Resource
    HouseService houseService;


    @ApiOperation("查询房屋信息")
    @RequestMapping("/selectHouse")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "houseid", value = "序号"),
            @ApiImplicitParam(name = "housedesc", value = "描述"),
            @ApiImplicitParam(name = "typename", value = "户型"),
            @ApiImplicitParam(name = "monthlyren", value = "租金"),
            @ApiImplicitParam(name = "publishdate", value = "发布日期"),
    })
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数问题"),
            @ApiResponse(code = 404, message = "请求页面没有或页面跳转路径不对"),
            @ApiResponse(code = 405, message = "请求类型不正确"),
            @ApiResponse(code = 500, message = "后台程序报错")
    })
    public ResultEntity<PageInfo<viewsInfoType>> selectHouse(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        PageInfo<viewsInfoType> pageInfo = houseService.selectAll(pageNum);
        return ResultEntity.successWithoutData(pageInfo);
    }

    @ApiOperation("查询房屋信息")
    @RequestMapping("/selectHouseType")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "typeid", value = "户型编号"),
            @ApiImplicitParam(name = "typename", value = "户型名"),
    })
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数问题"),
            @ApiResponse(code = 404, message = "请求页面没有或页面跳转路径不对"),
            @ApiResponse(code = 405, message = "请求类型不正确"),
            @ApiResponse(code = 500, message = "后台程序报错")
    })
    public ResultEntity<List<HouseType>> selectHouseType() {
        List<HouseType> typeList = houseService.selectAllHouseType();
        return ResultEntity.successWithoutData(typeList);
    }

    @ApiOperation("新增用户接口")
    @PostMapping(value = "/save")
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数问题"),
            @ApiResponse(code = 404, message = "请求页面没有或页面跳转路径不对"),
            @ApiResponse(code = 405, message = "请求类型不正确"),
            @ApiResponse(code = 500, message = "后台程序报错")
    })
    public ResultEntity<String> insert(@ApiParam(name = "houseInfo", value = "房屋管理", required = true) @RequestBody HouseInfo houseInfo) {
        try {
            Integer save = 0;
            if (houseInfo.getHouseid() == null) {
                save = houseService.saveHouse(houseInfo);
            } else {
                save = houseService.updateHouse(houseInfo);
            }
            return ResultEntity.successWithoutData(save + "");
        } catch (Exception ex) {
            return ResultEntity.failed(ex.getMessage());
        }
    }

    @ApiOperation("根据id查询房屋信息接口")
    @RequestMapping(value = "/getHouseById")
    @ApiImplicitParam(name = "houseid", value = "序号", required = true)
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数问题"),
            @ApiResponse(code = 404, message = "请求页面没有或页面跳转路径不对"),
            @ApiResponse(code = 405, message = "请求类型不正确"),
            @ApiResponse(code = 500, message = "后台程序报错")
    })
    public ResultEntity<HouseInfo> getHouseById(Integer houseid) {
        HouseInfo houseInfo = houseService.selectAllById(houseid);
        return ResultEntity.successWithoutData(houseInfo);
    }
}